package org.news.web;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.news.entity.Topic;
import org.news.service.impl.TopicsServiceImpl;

public class GlobalResourceInitListener implements ServletContextListener {
    private final Logger log = Logger
            .getLogger(GlobalResourceInitListener.class);

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        ServletContext application = arg0.getServletContext();
        try {
            List<Topic> topics = new TopicsServiceImpl().findAllTopics();
            log.info("初始化得到" + topics.size() + "个新闻主题");
            application.setAttribute("ALL_NEWS_TOPICS", topics);
        } catch (SQLException e) {
            log.error("新闻主题初始化错误");
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // 暂无销毁需求
    }
}
